<?php namespace Home\Controller; 

class ListController extends CommonController{
      public function index(){
      	$count = Db::table("article")->where("is_recycle",0)->count();
		  $page = Page::row(3)->make($count);	
    $cid = Q("get.cid",0,"intval");
		$tid = Q("get.tid",0,"intval");
      	if($cid){
      		//实例化分类模型
		$cateModel = new \Common\Model\Categroy;
			//左侧头部部分
			//产看分类的子类有多少
		$cids = $cateModel->getSon($cateModel->get(),$cid);
			//把自己压进去
			$cids[]=$cid;
//			$cids = implode(",", $cids);p($cids);
$headData = array("type"=>"分类","name"=>Db::table("Category")->where("cid",$cid)->pluck("cname"),"total"=>Db::table("article")->where("category_cid in (".implode(",",$cids).")") -> count());
               View::with("headData",$headData);
			
			  //左侧文章内容
			  $data = Db::table("article")->join("Category","category_cid","=","cid")->where("category_cid in (".implode(",",$cids).")")->where("is_recycle",0)->limit(page::limit())->get();
			  foreach ($data as $k => $v) {
			  	$data[$k]['tag'] = Db::table("article_tag")
				->join("tag","tid","=","tag_tid")
				->where("article_aid",$v['aid'])
				->lists("tid,tname");
			
			  }
		
		}
		
		
		
		
		//如果穿过来的是tid标签
		if($tid){
			$headData = 
			array(
			"type"=>"标签",
			"name"=>Db::table("tag")->where("tid",$tid)->pluck("tname"),
			"total"=>Db::table("tag")->join("article_tag","tid","=","tag_tid")->where("tag_tid",$tid)->count(),
			);

			View::with("headData",$headData);
			//这个标签的所有文章ID
			$aids = Db::table("article_tag")->where("tag_tid","=",$tid)->lists("article_aid");
			
			if($aids){
		
				//获得左侧具体内容
			  $data = Db::table("article")->join("Category","category_cid","=","cid")->where("aid in (".implode(",",$aids).")")->where("is_recycle",0)->limit(page::limit())->get();
				
		foreach ($data as $k => $v) {
				$data[$k]['tag'] = Db::table("tag")
				->join("article_tag","tid","=","tag_tid")
				->where("tag_tid",$tid)
				->lists("tid,tname");
				
			}
   }else{
		 	$data = array();
		 }
    
		}

	View::with("page",$page);
		View::with("data",$data);
      	View::make();
	}
	
}
